/**
 * Created by lizhe on 2018/4/17.
 */
var datas = [];
var first = [];
var second = [];
var third = [];

var menuId = 0;
var type = "";
var next = "";
$(document).ready(function(){
    menuId = getQueryString("menuId");
    type = getQueryString("type");
    next = getQueryString("next");
    queryList2(menuId);
    queryArticle(1,20,menuId);

    //翻页
    //$("#page").click(function(){
    //    var page = $("#page").data('pagination').currentPage + 1;
    //    queryArticle(page,20,menuId);
    //});
});


function queryArticle(page,pageSize,menuId){
    $.ajax({
        url : urladdr + "/web/article/list",
        method : "GET",
        data : {
            page:page,
            pageSize:pageSize,
            menuId:menuId,
            status:2
        },
        success : function(data){
            if(data.code == 0){
                $("#content").empty();
                if(data.data.list.length!=0){
                    $.each(data.data.list,function(i,date){
                        var ext = JSON.parse(date.ext);
                        //var myDate = new Date(ext.submitTime);
                        var myDate = new Date();
                        var myYear = myDate.getFullYear();
                        var myMonth = myDate.getMonth() + 1;
                        var myDay = myDate.getDate();

                        var tDate = new Date(date.addTime);
                        var tYear = tDate.getFullYear();
                        var tMonth = tDate.getMonth() + 1;
                        var tDay = tDate.getDate();

                        var content = date.content;
                        var reTag = /<img(?:.|\s)*?>/g;
                        var time = date.time;
                        content = content.replace(reTag,'');
                        content = content.replace(/&nbsp;/ig, "");
                        content = content.replace(/<[^>]*>|/g,"");

                        var img = date.imageUrl;
                        img = img.split("/");
                        var length = img.length;
                        var str = img[length-1];
                        if(myYear == tYear && myMonth == tMonth && myDay == tDay){
                            if(img[length-1]!="articleAdd.png"){
                                document.getElementById("content").innerHTML = document.getElementById("content").innerHTML +
                                    '<div class="newsDiv" onclick=toInfo("'+date.id+'")>'+
                                    '<img src="'+imgUrl+date.imageUrl+'" class="newsImg">'+
                                    '<div class="newsTitle">'+date.title+'<img src="images/new_01_002.png" style="margin-left: 16px"></div>'+
                                    '<div class="newsContent">'+content+'</div>'+
                                    '<div class="newsInfo">'+
                                    '<span class="newsItem">来源：'+ext.source+'</span>'+
                                    '<span class="newsItem">&nbsp;&nbsp;作者：'+ext.authorName+'&nbsp;&nbsp;</span>'+
                                    '<span class="newsItem">&nbsp;&nbsp;编辑人：'+date.userName+'&nbsp;&nbsp;</span>'+
                                    '<span class="newsItem">&nbsp;&nbsp;发布时间：'+ext.submitTime+'&nbsp;&nbsp;</span>'+
                                    '</div>'+
                                    '</div>'
                            }else{
                                document.getElementById("content").innerHTML = document.getElementById("content").innerHTML +
                                    '<div class="newsDiv" onclick=toInfo("'+date.id+'")>'+
                                    '<img src="'+imgUrl+date.imageUrl+'" class="newsImg" style="display: none">'+
                                    '<div class="newsTitle" style="margin-left: 16px">'+date.title+'<img src="images/new_01_002.png" style="margin-left: 16px"></div>'+
                                    '<div class="newsContent" style="margin-left: 16px">'+content+'</div>'+
                                    '<div class="newsInfo" style="margin-left: 16px">'+
                                    '<span class="newsItem">来源：'+ext.source+'</span>'+
                                    '<span class="newsItem">&nbsp;&nbsp;作者：'+ext.authorName+'&nbsp;&nbsp;</span>'+
                                    '<span class="newsItem">&nbsp;&nbsp;编辑人：'+date.userName+'&nbsp;&nbsp;</span>'+
                                    '<span class="newsItem">&nbsp;&nbsp;发布时间：'+ext.submitTime+'&nbsp;&nbsp;</span>'+
                                    '</div>'+
                                    '</div>'
                            }
                        }else{
                            if(img[length-1] != "articleAdd.png"){
                                document.getElementById("content").innerHTML = document.getElementById("content").innerHTML +
                                    '<div class="newsDiv" onclick=toInfo("'+date.id+'")>'+
                                    '<img src="'+imgUrl+date.imageUrl+'" class="newsImg">'+
                                    '<div class="newsTitle">'+date.title+'</div>'+
                                    '<div class="newsContent">'+content+'</div>'+
                                    '<div class="newsInfo">'+
                                    '<span class="newsItem">来源：'+ext.source+'</span>'+
                                    '<span class="newsItem">&nbsp;&nbsp;作者：'+ext.authorName+'&nbsp;&nbsp;</span>'+
                                    '<span class="newsItem">&nbsp;&nbsp;编辑人：'+date.userName+'&nbsp;&nbsp;</span>'+
                                    '<span class="newsItem">&nbsp;&nbsp;发布时间：'+ext.submitTime+'&nbsp;&nbsp;</span>'+
                                    '</div>'+
                                    '</div>'
                            }else{
                                document.getElementById("content").innerHTML = document.getElementById("content").innerHTML +
                                    '<div class="newsDiv" onclick=toInfo("'+date.id+'")>'+
                                    '<img src="'+imgUrl+date.imageUrl+'" class="newsImg" style="display: none">'+
                                    '<div class="newsTitle" style="margin-left: 16px">'+date.title+'</div>'+
                                    '<div class="newsContent" style="margin-left: 16px">'+content+'</div>'+
                                    '<div class="newsInfo" style="margin-left: 16px">'+
                                    '<span class="newsItem">来源：'+ext.source+'</span>'+
                                    '<span class="newsItem">&nbsp;&nbsp;作者：'+ext.authorName+'&nbsp;&nbsp;</span>'+
                                    '<span class="newsItem">&nbsp;&nbsp;编辑人：'+date.userName+'&nbsp;&nbsp;</span>'+
                                    '<span class="newsItem">&nbsp;&nbsp;发布时间：'+ext.submitTime+'&nbsp;&nbsp;</span>'+
                                    '</div>'+
                                    '</div>'
                            }
                        }
                    });
                    document.getElementById("content").innerHTML = document.getElementById("content").innerHTML +
                        '<div style="width: 90%" align="center">'+
                        '<div class="page" style="margin-top: 20px">'+
                        '<div class="pagination-holder clearfix">'+
                        '<div id="page" class="pagination" style="display: inline-block" onclick="page()"></div>'+
                        '</div>'+
                        '</div>'+
                        '</div>';
                    $("#page").pagination({
                        pages: data.data.totalPage,
                        cssStyle: 'light-theme',
                        displayedPages:5,
                        edges:2,
                        currentPage:page
                    });
                }else{

                }
            }else if(data.code == 401){
                location.href = "index.html"
            }else{
                modalinfo(data.msg);
            }
        }
    });
}

function toInfo(id){
    location.href = "text.html?menuId="+menuId + "&id="+id+"&next="+next;
}

function page(){
    var page = $("#page").data('pagination').currentPage + 1;
    queryArticle(page,20,menuId);
}